package com.credithc.signature.common.service;

import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.common.constant.CacheConstant;
import com.credithc.signature.common.constant.ManagerConstant;
import com.credithc.signature.common.mapper.ChannelMapper;
import com.credithc.signature.common.po.Channel;
import com.credithc.signature.common.ro.manager.ChannelRo;
import com.credithc.signature.common.utils.CacheUtil;
import com.credithc.signature.common.utils.ManagerBeanUtil;
import com.credithc.signature.common.utils.PageUtils;
import com.credithc.signature.common.vo.manager.ChannelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

@Service
@Slf4j
public class ChannelService {

    @Autowired
    private ChannelMapper channelMapper;


    /**
     * 获取渠道信息--缓存
     */
    public Channel selectCacheByChannelNo(String channelNo) {
        Channel cacheChannel = null;
        try {
            cacheChannel = CacheUtil.getChannelCache().get(CacheConstant.CHANNEL_PREFIX + channelNo);
        } catch (ExecutionException e) {
            log.error("获取渠道缓存失败......{}",e.getMessage());
        }
        return cacheChannel;
    }

    /**
     * 查询渠道信息
     * @param channelNo
     * @return
     */
    public Channel selectByChannelNo(String channelNo) {
        Channel channel = channelMapper.selectByChannelNo(channelNo);
        return channel;
    }

    /**
     * 新增渠道
     *
     * @param channelRo
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(ChannelRo channelRo) {
        Channel channel = new Channel();
        BeanUtils.copyProperties(channelRo, channel);
        channel.setChannelStatus(true);
        channel.setCreateTime(new Date());
        channel.setUpdateTime(new Date());
        log.info("存入数据库" + channel.toString());
        channelMapper.insert(channel);
    }

    /**
     * 查询所有
     *
     * @param channelRo
     * @return
     */
    public PageInfo<ChannelVo> queryAll(ChannelRo channelRo) {
        if (channelRo.getChannelNos() != null) {
            log.info("查询所有渠道channelNos=" + channelRo.getChannelNos());
            PageUtils.setPageNo(channelRo);
            List<Channel> channelPos = channelMapper.listByParamsAndPage(channelRo);
            int count = channelMapper.countByParamsAndPage(channelRo);
            List<ChannelVo> vos = buildPos(channelPos);
            return new PageInfo<>(vos, count, channelRo.getPageNo(), channelRo.getPageSize());
        } else {
            return new PageInfo<>(null, 0, channelRo.getPageNo(), channelRo.getPageSize());
        }
    }

    /**
     * 查询所有--不鉴权
     *
     * @param channelRo
     * @return
     */
    public PageInfo<ChannelVo> queryAllNoAuth(ChannelRo channelRo) {
        PageUtils.setPageNo(channelRo);
        List<Channel> channelPos = channelMapper.listByParamsAndPage(channelRo);
        int count = channelMapper.countByParamsAndPage(channelRo);
        List<ChannelVo> vos = buildPos(channelPos);
        return new PageInfo<>(vos, count, channelRo.getPageNo(), channelRo.getPageSize());
    }

    /**
     * 根据渠道号查询
     *
     * @param channelId
     * @return
     */
    public Channel selectChannelByCode(String channelId) {
        Channel channel = new Channel();
        channel.setChannelNo(channelId);
        Map<String, Object> params = ManagerBeanUtil.buildQueryParams(channel);
        return channelMapper.selectOneByExample(params.get(ManagerConstant.EXAMPLE));
    }


    //----------------------------------------工具方法----------------------------------------

    private List<ChannelVo> buildPos(List<Channel> channelPos) {
        List<ChannelVo> list = new ArrayList<>();
        channelPos.forEach(po -> {
            ChannelVo channelVo = new ChannelVo();
            list.add(convertPotoVo(po, channelVo));
        });
        return list;
    }

    private ChannelVo convertPotoVo(Channel po, ChannelVo channelVo) {
        BeanUtils.copyProperties(po, channelVo);
        return channelVo;
    }

    public void update(ChannelRo channelRo) {
        if (channelRo.getChannelNo() == null) {
            throw new RuntimeException("请提供产品号");
        }
        Channel channel = new Channel();
        BeanUtils.copyProperties(channelRo, channel);
        channel.setChannelStatus(true);
        channel.setUpdateTime(new Date());
        Example example = new Example(Channel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("channelNo", channelRo.getChannelNo());
        channelMapper.updateByExampleSelective(channel, example);
    }

    public void updateByPrimaryKeySelective(Channel channelPo) {
        channelMapper.updateByPrimaryKeySelective(channelPo);
    }
}
